//
//  AppDelegate.m
//  OCMicroBlog
//
//  Created by 002 on 15/11/4.
//  Copyright © 2015年 002. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "NetworkTools.h"
#import "UserAccountViewModel.h"
#import "NewFeatureViewController.h"
#import "NewFeatureViewLayout.h"
#import "WelcomeViewController.h"

@interface AppDelegate ()

// 新版标识
@property(nonatomic,assign) BOOL isNewVersion;

// 默认根控制器
@property(nonatomic,strong) UIViewController *defaultRootViewController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    
    // 设置根控制器
//    MainViewController *vc = [[MainViewController alloc] init];
//    self.window.rootViewController = vc;
    
//    self.window.rootViewController = [[NewFeatureViewController alloc] initWithCollectionViewLayout:[[NewFeatureViewLayout alloc] init]];
    self.window.rootViewController = self.defaultRootViewController;
    [self setupAppearane];
    
    
    // 注册通知
    [[NSNotificationCenter defaultCenter] addObserverForName:WBSwitchRootViewControllerNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        
        NSLog(@"%@---%@",[NSThread currentThread],note);
        self.window.rootViewController = [[MainViewController alloc] init];
    }];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


/**
 *  设置全局样式
 */
- (void)setupAppearane {
    
    // 全局渲染颜色
    [[UITabBar appearance] setTintColor:[UIColor orangeColor]];
    [[UINavigationBar appearance] setTintColor:[UIColor orangeColor]];
}

#pragma mark - 设置属性
// 是否为新版
- (BOOL)isNewVersion {
    
    // 1. 获取当前版本
    double currentVersion = ((NSString *)[NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"]).doubleValue;
    
    // 2. 从沙盒加载之前保存的版本
    NSString *sandboxVersionKey = @"sandboxVersionKey";
    double sandboxVersion = [[NSUserDefaults standardUserDefaults] doubleForKey:sandboxVersionKey];
    
    // 3. 将当前版本保存到沙盒中
    [[NSUserDefaults standardUserDefaults] setDouble:currentVersion forKey:sandboxVersionKey];
    
    NSLog(@"%lf",currentVersion);
    [self setupAppearane];
    
    // 4. 返回比较结果
    return currentVersion > sandboxVersion;
}

- (UIViewController *)defaultRootViewController {
    
    // 1. 判断是否登录
    if ([UserAccountViewModel sharedAccountViewModel].userLogin) {
        return (self.isNewVersion ? [[NewFeatureViewController alloc] initWithCollectionViewLayout:[[NewFeatureViewLayout alloc] init]] : [[WelcomeViewController alloc] init]);
    }
    return [[MainViewController alloc] init];
}


- (void)dealloc {
    
    // 销毁通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
